package com.sitia.jd.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sitia.common.CustomException;
import com.sitia.common.utils.DateUtils;
import com.sitia.common.utils.SecurityUtils;
import com.sitia.jd.domain.MxOrder;
import com.sitia.jd.mapper.MxOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sitia.jd.mapper.MxOrderHisMapper;
import com.sitia.jd.domain.MxOrderHis;
import com.sitia.jd.service.IMxOrderHisService;

/**
 * mx订单历史Service业务层处理
 * 
 * @author sitia
 * @date 2024-09-07
 */
@Service
public class MxOrderHisServiceImpl   extends ServiceImpl<MxOrderHisMapper, MxOrderHis>  implements IMxOrderHisService
{
    @Autowired
    private MxOrderHisMapper mxOrderHisMapper;
    @Autowired
    private MxOrderMapper mxOrderMapper;

    /**
     * 查询mx订单历史
     * 
     * @param hisId mx订单历史主键
     * @return mx订单历史
     */
    @Override
    public MxOrderHis selectMxOrderHisByHisId(Long hisId)
    {
        return mxOrderHisMapper.selectMxOrderHisByHisId(hisId);
    }

    /**
     * 查询mx订单历史列表
     * 
     * @param mxOrderHis mx订单历史
     * @return mx订单历史
     */
    @Override
    public List<MxOrderHis> selectMxOrderHisPageList(MxOrderHis mxOrderHis)
    {
        return mxOrderHisMapper.selectMxOrderHisPageList(mxOrderHis);
    }

    /**
     * 新增mx订单历史
     * 
     * @param mxOrderHis mx订单历史
     * @return 结果
     */
    @Override
    public int insertMxOrderHis(MxOrderHis mxOrderHis)
    {
        mxOrderHis.setCreateTime(DateUtils.getNowDate());
        return mxOrderHisMapper.insertMxOrderHis(mxOrderHis);
    }

    /**
     * 修改mx订单历史
     * 
     * @param mxOrderHis mx订单历史
     * @return 结果
     */
    @Override
    public int updateMxOrderHis(MxOrderHis mxOrderHis)
    {
        return mxOrderHisMapper.updateMxOrderHis(mxOrderHis);
    }

    /**
     * 批量删除mx订单历史
     * 
     * @param hisIds 需要删除的mx订单历史主键
     * @return 结果
     */
    @Override
    public int deleteMxOrderHisByHisIds(Long[] hisIds)
    {
        return mxOrderHisMapper.deleteMxOrderHisByHisIds(hisIds);
    }

    /**
     * 删除mx订单历史信息
     * 
     * @param hisId mx订单历史主键
     * @return 结果
     */
    @Override
    public int deleteMxOrderHisByHisId(Long hisId)
    {
        return mxOrderHisMapper.deleteMxOrderHisByHisId(hisId);
    }

    @Override
    public int archive(List<Date> archiverTime) {
        if (archiverTime.size() != 2) {
            throw new CustomException("传入时间数量不对");
        }
        Date startTime = archiverTime.get(0);
        Date endTime = archiverTime.get(1);
        Date now = DateUtils.getNowDate();
        String username = SecurityUtils.getUsername();
        Long userId = SecurityUtils.getUserId();
        int size = baseMapper.archiveOrder(startTime, endTime, username, userId);
        if (size == 0) {
            throw new CustomException("未存在历史数据");
        }
        return mxOrderMapper.deleteOrderByTime(startTime,endTime);
    }
}
